package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.DictType;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.dicttype.*;
import com.zmj.sy.mom.srv.aps.mapper.DictTypeMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@RequiredArgsConstructor
@Service
public class DictTypeService {

    private final DictTypeMapper dictTypeMapper;
    public void add(DictTypeAddReqVo reqVo) {
        DictType dt = new DictType();
        BeanUtils.copyProperties(reqVo, dt);
        dictTypeMapper.insert(dt);
    }

    public void edit(DictTypeEditReqVo reqVo) {
        DictType dt = new DictType();
        BeanUtils.copyProperties(reqVo, dt);
        dictTypeMapper.updateById(dt);
    }

    public void delete(BaseIdReqVo reqVo) {
        dictTypeMapper.deleteById(reqVo.getId());
    }

    public DictTypeListResVo list(DictTypeListReqVo reqVo) {
        QueryWrapper<DictType> query = Wrappers.query();
        query.like(StringUtils.hasText(reqVo.getDictName()), "dict_name", reqVo.getDictName());
        query.like(StringUtils.hasText(reqVo.getDictType()), "dict_type", reqVo.getDictType());
        query.eq(StringUtils.hasText(reqVo.getStatus()), "status", reqVo.getStatus());
        query.orderByDesc("create_date_time");
        Page<DictType> page = dictTypeMapper.selectPage(reqVo.toPage(DictType.class), query);
        DictTypeListResVo resVo = new DictTypeListResVo();
        resVo.setTotal(page.getTotal());
        resVo.setData(page.getRecords().stream().map(e-> {
            DictTypeListDataResVo data = new DictTypeListDataResVo();
            BeanUtils.copyProperties(e, data);
            return data;
        }).collect(java.util.stream.Collectors.toList()));
        return resVo;
    }

    public DictTypeGetResVo get(BaseIdReqVo reqVo) {
        DictType dictType = dictTypeMapper.selectById(reqVo.getId());
        DictTypeGetResVo resVo = new DictTypeGetResVo();
        BeanUtils.copyProperties(dictType, resVo);
        return resVo;
    }

}
